package com.openlogic.common.logic.utils.rpc;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

import com.alibaba.fastjson2.JSONObject;

/**
* @ClassName: WebServiceUtils
* @Description: WebService调用工具类
* @author weiyongpeng
* @date 2024年8月7日 上午10:49:06
 */
public class WebServiceUtils {

	// 创建动态客户端
	private static Client client = null;

	public static String getInvoke(String url, String functionName, Object[] wsParams) {
		JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
		// "http://127.0.0.1:8081/ws/user?wsdl"
		client = factory.createClient(url);
		HTTPConduit conduit = (HTTPConduit) client.getConduit();
		HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
		httpClientPolicy.setConnectionTimeout(2000); // 连接超时
		httpClientPolicy.setAllowChunking(false); // 取消块编码
		httpClientPolicy.setReceiveTimeout(120000); // 响应超时
		conduit.setClient(httpClientPolicy);
		String str = "";
		try {
			Object[] objects = null;
			// invoke("方法名",参数1,参数2,参数3....); 调用执行
			objects = client.invoke(functionName, wsParams);
			str = JSONObject.parseObject(objects[0].toString()).toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
}
